
// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}

/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
  if(!root) return [];
  if(root && !root.left && !root.right) return [root.val];
  const stack = [root], result = [];
  while(stack.length) {
    const node = stack.pop();
    result.push(node.val);
    if(node.right) stack.push(node.right);
    if(node.left) stack.push(node.left);
  }
  return result;
};
let root = [1,null,2,3];
console.log(preorderTraversal(root));
root = [];
console.log(preorderTraversal(root));
root = [1];
console.log(preorderTraversal(root));
root = [1,2];
console.log(preorderTraversal(root));
root = [1,null,2];
console.log(preorderTraversal(root));
